import type { Config } from 'svgo';
/*
 * @title:
 * @Description:
 * @Author: JZT.饶慧勇
 * @Date: 2025-05-27 18:53:05
 * @LastEditors: JZT.饶慧勇
 * @LastEditTime: 2025-06-10 19:00:42
 */
import uni from '@dcloudio/vite-plugin-uni';
import Components from '@uni-helper/vite-plugin-uni-components';
import { WotResolver } from '@uni-helper/vite-plugin-uni-components/resolvers';
import UniHelperLayouts from '@uni-helper/vite-plugin-uni-layouts';
import AutoImport from 'unplugin-auto-import/vite';
import { defineConfig, loadEnv } from 'vite';
import eslint from 'vite-plugin-eslint';
import svgLoader from 'vite-svg-loader';

const svgoConfig: Config = {
  plugins: [
    {
      name: 'preset-default',
      params: {
        overrides: {
          // customize default plugin options
          inlineStyles: {
            onlyMatchedOnce: false
          },

          // or disable plugins
          removeDoctype: false,
          removeViewBox: false // ViewBox默认不删除
        }
      }
    },
    {
      name: 'removeAttrs',
      params: { attrs: ['svg:width', 'svg:height'] } // 删除指定节点的属性
    },
    'convertStyleToAttrs', // 将样式转换为属性
    'removeStyleElement', // 删除 <style> 元素
    'prefixIds' // 用文件名或其他任意字符串作为元素 ID 和类名的前缀。
  ]
};

// https://cn.vite.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  console.log('env', env);

  return {
    define: {},
    css: {
      modules: {
        localsConvention: 'camelCase' // 默认只支持驼峰，修改为同时支持横线和驼峰
      },
      postcss: {
        plugins: [
          {
            postcssPlugin: 'internal:charset-removal',
            AtRule: {
              charset: atRule => {
                if (atRule.name === 'charset') {
                  atRule.remove();
                }
              }
            }
          }
        ]
      },
      preprocessorOptions: {
        scss: {
          api: 'modern-compiler', // https://github.com/element-plus/element-plus/pull/18579
          //      // TODO: After SASS warnings fixed in element plus, remove this line.
          quietDeps: true, // Silence deprecation warnings from dependencies
          javascriptEnabled: true,
          additionalData: '@use "@/assets/styles/variables.scss" as *;'
        }
      }
    },
    optimizeDeps: {
      include: ['vue', 'pinia', 'lodash-es']
    },
    plugins: [
      svgLoader({ svgoConfig }),
      eslint({
        exclude: ['**/node_modules/**', 'dist/**']
      }),
      UniHelperLayouts(),
      Components({
        resolvers: [WotResolver()],
        dts: 'src/components.d.ts',
        directoryAsNamespace: true
      }),
      AutoImport({
        eslintrc: {
          enabled: true,
          filepath: 'src/.eslintrc-auto-import.json'
        },
        imports: [
          'vue',
          'uni-app',
          'pinia',
          '@vueuse/core',
          {
            'wot-design-uni': ['useToast', 'useMessage', 'useUpload', 'useCountDown']
          }
        ],
        dts: 'src/auto-imports.d.ts',
        dirs: ['src/stores', 'src/utils', 'src/hooks'],
        vueTemplate: false // TODO: 统一使用easycom组件规范 https://zh.uniapp.dcloud.io/collocation/pages.html#easycom
      }),
      uni()
    ].filter(Boolean)
  };
});
